# ———————————————————————–
# <copyright file="IIS.ps1" company="The MITRE Corporation">
#    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#    http:#www.apache.org/licenses/LICENSE-2.0
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
# </copyright>
# ———————————————————————–
################################################/
# IIS.ps1 - Creates IIS Directory Structure and configures IIS
# Project: IC.NET Installer
# Author: Don McGarry
################################################/

# Copyright (c) 2013 The MITRE Corporation. All rights reserved.
#
# NOTICE
# This software was produced for the U. S. Government
# under Contract No. FA8721-09-C-0001, and is
# subject to the Rights in Noncommercial Computer Software
# and Noncommercial Computer Software Documentation Clause
# (DFARS) 252.227-7014 (JUN 1995)

$Username=$Args[0]
$Password=$Args[1]
$icnetIISPath =$Args[2];
$IISSite=$Args[3];
$RESTAppPool=$Args[4];
$ServiceAppPool=$Args[5];
$Port=$Args[6]
$InstallerRoot=$Args[7]
$IISSvcSite=$Args[8]
$icnetIISSvcPath=$Args[9]
$SvcPort=$Args[10]

#Register IIS
&c:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i

#Remove IIS Default Stuff for system hardening
if((get-website | where-object { $_.name -eq 'Default Web Site' }) -ne $NULL)
{
	Remove-Website -Name "Default Web Site"
}

if(Test-Path 'IIS:\AppPools\ASP.NET v4.0')
{
	Remove-WebAppPool -Name "ASP.NET v4.0"
}

if(Test-Path 'IIS:\AppPools\ASP.NET v4.0 Classic')
{
	Remove-WebAppPool -Name "ASP.NET v4.0 Classic"
}

if(Test-Path 'IIS:\AppPools\Classic .NET AppPool')
{
	Remove-WebAppPool -Name "Classic .NET AppPool"
}

if(Test-Path 'IIS:\AppPools\DefaultAppPool')
{
	Remove-WebAppPool -Name "DefaultAppPool"
}

if(Test-Path 'IIS:\AppPools\.NET v4.5')
{
	Remove-WebAppPool -Name ".NET v4.5"
}

if(Test-Path 'IIS:\AppPools\.NET v4.5 Classic')
{
	Remove-WebAppPool -Name ".NET v4.5 Classic"
}

#Create the Directory Structure
if(!(Test-Path $icnetIISPath))
{
	md $icnetIISPath
}

if(!(Test-Path $icnetIISPath"bin"))
{
	md $icnetIISPath"bin"
}

if(!(Test-Path $icnetIISPath"Images"))
{
	md $icnetIISPath"Images"
}

if(!(Test-Path $icnetIISPath"lib"))
{
	md $icnetIISPath"lib"
}

if(!(Test-Path $icnetIISPath"Resources"))
{
	md $icnetIISPath"Resources"
}

if(!(Test-Path $icnetIISPath"Scripts"))
{
	md $icnetIISPath"Scripts"
}

if(!(Test-Path $icnetIISPath"Styles"))
{
	md $icnetIISPath"Styles"
}

if(!(Test-Path $icnetIISPath"Styles\images"))
{
	md $icnetIISPath"Styles\images"
}

if(!(Test-Path $icnetIISPath"XSLT"))
{
	md $icnetIISPath"XSLT"
}

if(!(Test-Path $icnetIISSvcPath))
{
	md $icnetIISSvcPath
}

if(!(Test-Path $icnetIISSvcPath"bin"))
{
	md $icnetIISSvcPath"bin"
}

if(!(Test-Path $icnetIISSvcPath"DeadLetterService"))
{
	md $icnetIISSvcPath"DeadLetterService"
}

if(!(Test-Path $icnetIISSvcPath"DeadLetterService\bin"))
{
	md $icnetIISSvcPath"DeadLetterService\bin"
}

if(!(Test-Path $icnetIISSvcPath"DEProcessingService"))
{
	md $icnetIISSvcPath"DEProcessingService"
}

if(!(Test-Path $icnetIISSvcPath"DEProcessingService\bin"))
{
	md $icnetIISSvcPath"DEProcessingService\bin"
}

if(!(Test-Path $icnetIISSvcPath"ExceptionService"))
{
	md $icnetIISSvcPath"ExceptionService"
}

if(!(Test-Path $icnetIISSvcPath"ExceptionService\bin"))
{
	md $icnetIISSvcPath"ExceptionService\bin"
}

if(!(Test-Path $icnetIISSvcPath"FederationService"))
{
	md $icnetIISSvcPath"FederationService"
}

if(!(Test-Path $icnetIISSvcPath"FederationService\bin"))
{
	md $icnetIISSvcPath"FederationService\bin"
}

if(!(Test-Path $icnetIISSvcPath"Images"))
{
	md $icnetIISSvcPath"Images"
}

if(!(Test-Path $icnetIISSvcPath"KeywordRoutingService"))
{
	md $icnetIISSvcPath"KeywordRoutingService"
}

if(!(Test-Path $icnetIISSvcPath"KeywordRoutingService\bin"))
{
	md $icnetIISSvcPath"KeywordRoutingService\bin"
}

if(!(Test-Path $icnetIISSvcPath"lib"))
{
	md $icnetIISSvcPath"lib"
}

if(!(Test-Path $icnetIISSvcPath"Resources"))
{
	md $icnetIISSvcPath"Resources"
}

if(!(Test-Path $icnetIISSvcPath"Scripts"))
{
	md $icnetIISSvcPath"Scripts"
}

if(!(Test-Path $icnetIISSvcPath"Styles"))
{
	md $icnetIISSvcPath"Styles"
}

if(!(Test-Path $icnetIISSvcPath"Styles\images"))
{
	md $icnetIISSvcPath"Styles\images"
}

if(!(Test-Path $icnetIISSvcPath"XSLT"))
{
	md $icnetIISSvcPath"XSLT"
}

#CHECK THIS...
cp $InstallerRoot\Lib\*.* $icnetIISPath
cp $InstallerRoot\Lib\*.* $icnetIISSvcPath

#Create IIS App Pools
if(!(Test-Path "IIS:\AppPools\$ServiceAppPool"))
{
	New-WebAppPool -Name $ServiceAppPool
}

Set-ItemProperty IIS:\AppPools\$ServiceAppPool managedRuntimeVersion v4.0
Set-ItemProperty IIS:\apppools\$ServiceAppPool -name processModel -value @{userName=$Username;password=$Password;identitytype=3}

if(!(Test-Path "IIS:\AppPools\$RESTAppPool"))
{
	New-WebAppPool -Name $RESTAppPool
}

Set-ItemProperty IIS:\AppPools\$RESTAppPool managedRuntimeVersion v4.0
Set-ItemProperty IIS:\apppools\$RESTAppPool -name processModel -value @{userName=$Username;password=$Password;identitytype=3}

#Create Base Router Website
if((get-website | where-object { $_.name -eq $IISSite }) -eq $NULL)
{
	#This is a fix for when there are no websites on the server, need to set the ID manually
	$id = (dir iis:\sites | foreach {$_.id} | sort -Descending | select -first 1) + 1
	New-Website -Name $IISSite -Port $Port -ApplicationPool $RESTAppPool -PhysicalPath $icnetIISPath -id $id
}

if((get-website | where-object { $_.name -eq $IISSvcSite }) -eq $NULL)
{
	#This is a fix for when there are no websites on the server, need to set the ID manually
	$id = (dir iis:\sites | foreach {$_.id} | sort -Descending | select -first 1) + 1
	New-Website -Name $IISSvcSite -Port $SvcPort -ApplicationPool $ServiceAppPool -PhysicalPath $icnetIISSvcPath -id $id
	New-ItemProperty IIS:\Sites\$IISSvcSite -name bindings -value @{protocol="net.msmq";bindingInformation="localhost"}
}

#Create Applications for each service in SOA
if((Get-WebApplication -Name "DEProcessingService") -eq $NULL)
{
	New-WebApplication -Name DEProcessingService -Site $IISSvcSite -PhysicalPath $icnetIISSvcPath"DEProcessingService" -ApplicationPool $ServiceAppPool
}

if((Get-WebApplication -Name "ExceptionService") -eq $NULL)
{
	New-WebApplication -Name ExceptionService -Site $IISSvcSite -PhysicalPath $icnetIISSvcPath"ExceptionService" -ApplicationPool $ServiceAppPool
}

if((Get-WebApplication -Name "KeywordRoutingService") -eq $NULL)
{
	New-WebApplication -Name KeywordRoutingService -Site $IISSvcSite -PhysicalPath $icnetIISSvcPath"KeywordRoutingService" -ApplicationPool $ServiceAppPool
}

if((Get-WebApplication -Name "FederationService") -eq $NULL)
{
	New-WebApplication -Name FederationService -Site $IISSvcSite -PhysicalPath $icnetIISSvcPath"FederationService" -ApplicationPool $ServiceAppPool
}
if((Get-WebApplication -Name "DeadLetterService") -eq $NULL)
{
	New-WebApplication -Name DeadLetterService -Site $IISSvcSite -PhysicalPath $icnetIISSvcPath"DeadLetterService" -ApplicationPool $ServiceAppPool
}

#Enable MSMQ and HTTP on all applications
Set-ItemProperty "IIS:\Sites\$IISSvcSite\DEProcessingService" -Name enabledProtocols -Value "http,net.msmq"
Set-ItemProperty "IIS:\Sites\$IISSvcSite\ExceptionService" -Name enabledProtocols -Value "http,net.msmq"
Set-ItemProperty "IIS:\Sites\$IISSvcSite\KeywordRoutingService" -Name enabledProtocols -Value "http,net.msmq"
Set-ItemProperty "IIS:\Sites\$IISSvcSite\FederationService" -Name enabledProtocols -Value "http,net.msmq"
Set-ItemProperty "IIS:\Sites\$IISSvcSite\DeadLetterService" -Name enabledProtocols -Value "http,net.msmq"

#Copy in the svc files and web.config files
cp $InstallerRoot\IIS\DEProcessing.svc $icnetIISSvcPath"DEProcessingService\"
cp $InstallerRoot\IIS\DEWeb.config $icnetIISSvcPath"DEProcessingService\Web.config"
cp $InstallerRoot\IIS\Exception.svc $icnetIISSvcPath"ExceptionService\"
cp $InstallerRoot\IIS\ExceptionWeb.config $icnetIISSvcPath"ExceptionService\Web.Config"
cp $InstallerRoot\IIS\KeywordRouting.svc $icnetIISSvcPath"KeywordRoutingService\"
cp $InstallerRoot\IIS\KeywordWeb.Config $icnetIISSvcPath"KeywordRoutingService\Web.Config"
cp $InstallerRoot\IIS\Federation.svc $icnetIISSvcPath"FederationService\"
cp $InstallerRoot\IIS\FederationWeb.Config $icnetIISSvcPath"FederationService\Web.Config"
cp $InstallerRoot\IIS\DeadLetter.svc $icnetIISSvcPath"DeadLetterService\"
cp $InstallerRoot\IIS\DeadLetterWeb.Config $icnetIISSvcPath"DeadLetterService\Web.Config"
cp $InstallerRoot\IIS\crossdomain.xml $icnetIISSvcPath

#Set the application pools to always running for faster response
Set-ItemProperty IIS:\AppPools\$RESTAppPool -Name startMode -Value 1
Set-ItemProperty IIS:\AppPools\$ServiceAppPool -Name startMode -Value 1

#Add URL reservation for the core router service
netsh http add urlacl url=http://+:$Port/RoutingService user="NETWORK SERVICE"
netsh http add urlacl url=http://+:$SvcPort/RoutingService user="NETWORK SERVICE"